package cn.kgc.dao;

import cn.kgc.entity.Student;
import cn.kgc.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Rock
 * @create 2020-08-22 9:20
 */
public class StudentDaoImpl implements StudentDao {

    private Connection con = null;
    private PreparedStatement ps = null;

    //重写父类接口中的增加学生的方法
    @Override
    public void addStudent(Student s) {
        try {
            //获取数据库连接对象
            con = JDBCUtils.getConnection();
            //添加新增的SQL语句，数据库中的id为自增故而不进行插入
            String sql = "insert into student values(null, ?, ?)";
            //获取SQL语句的发送对象
            ps = con.prepareStatement(sql);
            //确定？问号处的数据
            ps.setString(1, s.getName());
            ps.setInt(2, s.getAge());
            //发送SQL语句
            int i = ps.executeUpdate();
            if (i>0){
                System.out.println("添加学生"+s.getName()+"成功");
            }else {
                System.out.println("添加学生"+s.getName()+"失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException();
        }finally {
            JDBCUtils.close(ps , con);
        }
    }
    //重写接口中删除学生的方法
    @Override
    public void deleteStudent(int sid) {
        try {
            con = JDBCUtils.getConnection();
            String sql = "delete from student where id = ?";
            ps = con.prepareStatement(sql);
            ps.setInt(1, sid);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException();
        }finally {
            JDBCUtils.close(ps , con);
        }
    }
    //重写接口中修改学生信息方法
    @Override
    public void updateStudent(Student s) {
        try {
            con = JDBCUtils.getConnection();
            String sql = "update student set name = ? ,age = ? where id = ?";
            ps = con.prepareStatement(sql);
            ps.setString(1, s.getName());
            ps.setInt(2, s.getAge());
            ps.setInt(3, s.getId());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException();
        }finally {
            JDBCUtils.close(ps , con);
        }
    }
    //重写接口中查询所有学生信息方法
    @Override
    public List<Student> getAllStudent() {
        ArrayList<Student> list = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "select * from student";
            list = new ArrayList<Student>();
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                int id = rs.getInt(1);
                String name = rs.getString(2);
                int age = rs.getInt(3);
                Student stu = new Student(id, name, age);
                list.add(stu);
            }
        } catch (SQLException e) {
            throw new RuntimeException();
        }finally {
            JDBCUtils.close(rs, ps, con);
        }
        //如果没有异常将返回student对象的集合
        return list;
    }
    //重写接口中获取指定id号学生记录信息
    @Override
    public Student getStudent(int sid) {
        ResultSet rs = null;
        Student s = null;
        try {
            //获取数据库连接
            con = JDBCUtils.getConnection();
            //获取sql语句
            String sql = "select * from student where id = ?";
            //获取发送对象
            ps = con.prepareStatement(sql);
            //处理？处信息
            ps.setInt(1, sid);
            //发送sql语句信息
            rs = ps.executeQuery();
            //解析结果集
            //游标需要移动才能获取经过的值
            rs.next();
            int id = rs.getInt(1);
            String name = rs.getString(2);
            int age = rs.getInt(3);
            s = new Student(id, name, age);
        } catch (SQLException e) {
            throw new RuntimeException();
        }finally{
            //调用工具类方法，关闭与数据库的连接
            JDBCUtils.close(rs, ps, con);
        }

        return s;
    }
}
